package com.mbuswidget;

import java.util.ArrayList;
import java.util.Collections;

import com.ericharlow.DragNDrop.DragListener;
import com.ericharlow.DragNDrop.DragNDropAdapter;
import com.ericharlow.DragNDrop.DragNDropListView;
import com.ericharlow.DragNDrop.DropListener;
import com.ericharlow.DragNDrop.RemoveListener;

import android.app.Activity;
import android.app.ListActivity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MBusWidgetConfigurationStops extends ListActivity {
	private int widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
	ArrayList<String> stopList = null;
	SharedPreferences prefs;
	ListView listView;
	DragNDropAdapter listAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widgetconfigstops);

		widgetID = getIntent().getIntExtra(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		if(widgetID == AppWidgetManager.INVALID_APPWIDGET_ID) {
			//Log.w("mbuswidget", "No Widget ID Specified");
			finish();
		}

		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		stopList = MBusWidgetConstants.GetWidgetStopList(prefs, widgetID);

		listAdapter = new DragNDropAdapter(this,
				new int[] { R.layout.dragitem }, new int[] { R.id.TextView01 },
				stopList);
		setListAdapter(listAdapter);// new DragNDropAdapter(this,content)
		listView = getListView();

		if (listView instanceof DragNDropListView) {
			((DragNDropListView) listView).setDropListener(mDropListener);
			((DragNDropListView) listView).setRemoveListener(mRemoveListener);
			((DragNDropListView) listView).setDragListener(mDragListener);
		}

		// Configure Top Buttons
		Button addStopButton = (Button) findViewById(R.id.addStopBtn);
		addStopButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent addStop = new Intent(MBusWidgetConfigurationStops.this,
						MBusWidgetConfigurationStopsAddStop.class);
				addStop.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				startActivityForResult(addStop, 0);
			}
		});

		Button removeStopButton = (Button) findViewById(R.id.removeStopBtn);
		removeStopButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent addStop = new Intent(MBusWidgetConfigurationStops.this,
						MBusWidgetConfigurationStopsRemoveStop.class);
				addStop.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
				startActivityForResult(addStop, 0);
			}
		});

	}

	private DropListener mDropListener = new DropListener() {
		public void onDrop(int from, int to) {
			ListAdapter adapter = getListAdapter();
			if (adapter instanceof DragNDropAdapter) {
				((DragNDropAdapter) adapter).onDrop(from, to);
				getListView().invalidateViews();

				// Save to Shared Preferences
				MBusWidgetConstants
						.SetWidgetStopList(prefs, widgetID, stopList);
			}
		}
	};

	private RemoveListener mRemoveListener = new RemoveListener() {
		public void onRemove(int which) {
			ListAdapter adapter = getListAdapter();
			if (adapter instanceof DragNDropAdapter) {
				((DragNDropAdapter) adapter).onRemove(which);
				getListView().invalidateViews();
			}
		}
	};

	private DragListener mDragListener = new DragListener() {

		int backgroundColor = 0xe0103010;
		int defaultBackgroundColor;

		public void onDrag(int x, int y, ListView listView) {
			// TODO Auto-generated method stub
		}

		public void onStartDrag(View itemView) {
			itemView.setVisibility(View.INVISIBLE);
			defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
			itemView.setBackgroundColor(backgroundColor);
			ImageView iv = (ImageView) itemView.findViewById(R.id.ImageView01);
			if (iv != null)
				iv.setVisibility(View.INVISIBLE);
		}

		public void onStopDrag(View itemView) {
			itemView.setVisibility(View.VISIBLE);
			itemView.setBackgroundColor(defaultBackgroundColor);
			ImageView iv = (ImageView) itemView.findViewById(R.id.ImageView01);
			if (iv != null)
				iv.setVisibility(View.VISIBLE);
		}

	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case (0): {
			if (resultCode == Activity.RESULT_OK) {
				// We have information required. Complete Widget setup.

				int stop_enum = data.getIntExtra(MBusWidgetConstants.STOP_TYPE,
						0);
				if (stop_enum != 0) {
					if (stop_enum == MBusWidgetConstants.ADD_STOP) {
						String[] addList = data.getExtras().getStringArray(
								MBusWidgetConstants.INTENT_SELECTED_STOPS);
						if(addList == null)
							return;
						Collections.addAll(stopList, addList);
						MBusWidgetConstants.SetWidgetStopList(prefs, widgetID,
								stopList);
						listAdapter.notifyDataSetChanged();
					} else if (stop_enum == MBusWidgetConstants.REMOVE_STOP) {
						int[] removeList = data.getExtras().getIntArray(MBusWidgetConstants.INTENT_SELECTED_STOPS);
						for (int i = removeList.length - 1; i >= 0; i--) {
							stopList.remove(removeList[i]);
						}
						
						MBusWidgetConstants.SetWidgetStopList(prefs, widgetID,
								stopList);
						listAdapter.notifyDataSetChanged();
					}
				}
			}
			break;
		}
		}
	}
}
